package com.example.microinvestcommon.util;
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.InputStream;

//阿里Oss上传图片转换Url

public class AliOssUtil {
    private static final String Endpoint= "https://oss-cn-shanghai.aliyuncs.com";
    private static final String AccessKey_ID="LTAI5tH3WUQgJnmAX6r3Qqpw";
    private static final String AccessKey_Secret="ctvcuuYzfOY9PJfJE0Bo2QL0ZLe5iM";
    private static final String Bucket_Name = "tianwailaiwu-microinvest";

    public static String uploadFile(String objectName, InputStream in) throws Exception {
        // 填写Bucket所在地域。以华东1（杭州）为例，Region填写为cn-hangzhou。
        String region = "cn-shanghai";
        // 创建OSSClient实例。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);

        CredentialsProvider credentialsProvider1=new DefaultCredentialProvider(AccessKey_ID,AccessKey_Secret);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(Endpoint)
                .credentialsProvider(credentialsProvider1)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();
        String URL="";
        try {
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(Bucket_Name, objectName, in);

            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);
            // 上传字符串。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
            URL="https://"+Bucket_Name+"."+Endpoint.substring(Endpoint.lastIndexOf("/")+1)+"/"+objectName;

        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return URL;
    }
}